import { observable, action } from 'mobx';
import * as API from '../../apis/monitor';
import CommonStore from '../CommonStore';
import { WeaTableNew, WeaForm, WeaLogView } from  'comsMobx';
import {Modal} from 'antd';
const { TableStore } = WeaTableNew;
const {LogStore} = WeaLogView;
import {WeaTools,WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
export default class MonitorStore extends CommonStore{

  @observable tableStore = new TableStore();
  @observable logStore = new LogStore();
  @observable form = new WeaForm();
  @observable condition = [];
  @observable showSearchAd = false;//高级搜索面板显示、隐藏
  @observable selectedRowKeys = [];
  @observable selectedRows = [];
  @observable workPlanId = '';
  @observable visibleCreate = false;
  @observable activeKey = '1';
  @observable visibleLog = false;
  @observable logSmallType = '';
  @observable logType = '';
  @observable alertPage = false;
  @observable sessionKey = '';
  @observable timeSag = 0;

  @observable loading = false;

  @action
  setLoading(newBool=false) {
      this.loading = newBool
  }

  constructor () {
    super();
    this.setState = this.setState.bind(this);
    this.getMonitor = this.getMonitor.bind(this);
    this.getCondition = this.getCondition.bind(this);
    this.operation = this.operation.bind(this);
  }

  @action
  setState(params = {}) {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    return this;
  }

  getMonitor(params = {}) {
    this.setLoading(true);
    if(params.beginDate != undefined || params.beginDaten != undefined || params.endDate != undefined || params.endDaten != undefined){
      this.timeSag = 0;
    }
    params = {...params,timeSag: this.timeSag}
    API.getMonitor(params).then(data => {
      if(data.ret == 'noright'){
        this.alertPage = true;
        this.setLoading(false);
        this.sessionKey = ''
      }else{
        this.alertPage = false;
        data.api_status && this.tableStore.getDatas(data.sessionkey, 1);
        this.sessionKey = data.sessionkey
        this.logType=data.logType;
        this.logSmallType=data.logSmallType;
        this.setLoading(false);
      }
      
    });
  }

  getCondition(params = {}) {
    API.getCondition(params).then(data => {
      if(data.api_status) {
        this.form.initFormFields(data.condition);
        this.condition = data.condition;
      }
    });
  }

  operation(params = {}) {
    API.operation(params).then(data => {
      if(data.api_status){
        this.selectedRowKeys = [];
        this.selectedRows=[];
        this.tableStore = new TableStore();
        this.getMonitor(this.form.getFormParams());
        // this.form.reset();
      } 
    });
  }

  exportAll = ()=>{
    WeaTools.callApi('/api/ec/dev/table/counts', 'POST', {dataKey:this.sessionKey}).then((result)=>{
      if(result.status){
        if(result.count > 10000){
          this.setModal(getLabel(508434,'目前支持的最大导出数据量为10000条,请修改限制条件,勿超过此数量。'))
        }else{
          this.tableStore.exportAll();
        }
      }
    })
  }

  setModal = (content) => {
    Modal.warning({
      title: `${getLabel(15172,'系统提示')}`,
      content: content,
    });
  }

  close = () => {
    this.setState({ visibleCreate: false, activeKey: '1' });
    this.status = { ...this.status, visibleCreate: false, activeKey: '1' }
    this.doUpdateCalender(true);
  }
  doUpdateCalender = (type) => {
    if (type == true) {
      this.setState({ update: true });
      this.status = { ...this.status, update: true };
    } else {
      this.setState({ update: false });
      this.status = { ...this.status, update: false };
    }
  }
  onlyClose = () => {
    this.setState({ visibleCreate: false, activeKey: '1' });
    this.status = { ...this.status, visibleCreate: false, activeKey: '1' }
  }

}